package com.shwread.android.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.shwread.android.qysw10000182.R;


public class RegisterSexDialog
        extends
        Dialog
{
	public interface onSelectSexListener
	{
		void onSelect(int sex);
	}
	
	private View        vMale, vFemale;
	
	private Context     context;
	private ImageView   vMaleSelected;
	private ImageView   vFemaleSelected;
	private int         sex;
	private SexListener listener;
	
	public RegisterSexDialog(Context context, int sex, SexListener listener) {
		super(context, R.style.common_dialog);
		this.sex = sex;
		this.listener = listener;
		setCanceledOnTouchOutside(true);
		
		this.context = context;
		
		findViews();
		setListeners();
		
		getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		getWindow().setGravity(Gravity.BOTTOM);
		getWindow().setWindowAnimations(android.R.style.Animation_InputMethod);
	}
	
	private void findViews() {
		View view = LayoutInflater.from(context).inflate(R.layout.personal_info_sexy, null);
		
		vMale = view.findViewById(R.id.male_rl);
		vFemale = view.findViewById(R.id.female_rl);
		vMaleSelected = (ImageView) view.findViewById(R.id.male_selected);
		vFemaleSelected = (ImageView) view.findViewById(R.id.female_selected);
		vMaleSelected.setVisibility(sex == 0 ? View.VISIBLE : View.GONE);
		vFemaleSelected.setVisibility(sex == 1 ? View.VISIBLE : View.GONE);
		setContentView(view);
	}
	
	private void setListeners() {
		ClickListener l = new ClickListener();
		
		vMale.setOnClickListener(l);
		vFemale.setOnClickListener(l);
		
	}
	
	void clickMale() {
		vMaleSelected.setVisibility(View.VISIBLE);
		vFemaleSelected.setVisibility(View.GONE);
		listener.onSelect(0);
		dismiss();
		
	}
	
	void clickFemale() {
		vFemaleSelected.setVisibility(View.VISIBLE);
		vMaleSelected.setVisibility(View.GONE);
		listener.onSelect(1);
		dismiss();
		
	}
	
	void clickCancel() {
		dismiss();
	}
	
	private class ClickListener
	        implements
	        View.OnClickListener
	{
		
		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
				case R.id.male_rl:
					clickMale();
					break;
				case R.id.female_rl:
					clickFemale();
					break;
			}
		}
	}
	
}
